import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
from alien import Alien
from game_stats import GameStats
from button import Button
from scoreboard import Scoreboard


def run_game():
    #创建一个窗体
    pygame.init()
    # 创建配置对象
    ai_settings = Settings()
    # 设置窗体宽高
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
    # 设置标题
    pygame.display.set_caption("外星人入侵")
    # 创建飞船对象
    ship = Ship(ai_settings,screen)
    # 创建子弹数组 空数组
    bullets = Group()
    # 创建一个外星人
    aliens = Group()
    gf.create_fleet(ai_settings,screen,ship,aliens)
    #  创建一个用于存储游戏统计信息的实例
    stats = GameStats(ai_settings)
    play_button = Button(ai_settings,screen,"Play game")
    # 得分板
    sb = Scoreboard(ai_settings, screen, stats)
    # 开始游戏的主循环
    while True:
        # pygame.time.Clock().tick(60)
        # 遍历所有事件
        # 事件就是鼠标和键盘所进行的操作
        # 窗口事件处理
        gf.check_events(ai_settings, screen, stats,sb, play_button, ship, aliens,bullets)
        if stats.game_active == True:
            ship.updata()
            gf.update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets)
            gf.update_bullets(ai_settings,screen,stats,sb,ship,aliens,bullets)
        # 游戏画面更新
        gf.update_screen(ai_settings,screen,stats,ship,bullets,aliens,play_button,sb)
run_game()